<?php
	class CompanywxAction extends Action {
		
		public $type;
		public $fromuser;
		public $touser;
		public $keyword;
		public $cid;
		public $message;
		
		public function _initialize() {
			$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$post = (array)$postObj;
			$this->fromuser = $post['FromUserName'];
			$this->touser = $post['ToUserName'];
			$this->keyword = trim($post['Content']);
			$this->type = $post['MsgType'];
			$this->event = $post['Event'];
			$this->eventkey = $post['EventKey'];
			$this->mediaid = $post['MediaId'];
			$this->picurl = $post['PicUrl'];
			$this->format = $post['Format'];
			$this->map_x = $post['Latitude'];
			$this->map_y = $post['Longitude'];
			$this->map_xx = $post['Location_X'];
			$this->map_yy = $post['Location_Y'];
			$this->cid = $_GET['cid'];
			import('ORG.Util.weixin');
			$this->message = new ResponsePassive();	
		}
		
		public function access(){
			ob_clean();
			//实现绑定微信公众号
			if(!empty($_GET['echostr'])){
				exit($_GET['echostr']);
			}
			//判断是否企业管理员微信号绑定
			if(($this->event=='subscribe'||$this->event=='SCAN')&&!empty($this->eventkey)){
				D('Weixin')->bind_companywx($this->eventkey,$this->fromuser,$this->touser);
			}
			/*//判断关注发送提示语
			if($this->event == 'subscribe'){
				$dato = array('0'=>'gh_9ebd33e20e57','1'=>'gh_7f2b372fd8c7','2'=>'gh_87ed782c66df');
				if(in_array($this->touser,$dato)){
					D('Weixin')->send_phone($this->fromuser,$this->touser);
				}else{
					$wel = D('company_wx')->where(array('wx_id'=>$this->touser))->find();
					if($wel['welcome'] !=""){
						D('Weixin')->send_welcome($this->fromuser,$this->touser,$wel['welcome']);
					}
				}
				
			}*/
			//判断关注发送提示语
			if($this->event == 'subscribe'){
				
					$wel = D('company_wx')->where(array('wx_id'=>$this->touser))->find();
					if($wel['welcome'] !=""){
						D('Weixin')->send_welcome($this->fromuser,$this->touser,$wel['welcome']);
					}
			}
			//自定义回复接口，包括文字和图文
			if($this->type == 'text'&&empty($this->event)){
				if($this->keyword=="微官网管理"){
					D('Weixin')->diycontrol($this->fromuser,$this->touser);
				}elseif($this->keyword=="修改照片"){
					D('Weixin')->uploadface($this->fromuser,$this->touser);
				}elseif($this->keyword=="修改语音简历"){
					D('Weixin')->uploadvoice($this->fromuser,$this->touser);
				}else{
					//企业名称优先判断
					D('Weixin')->diyindex_companywx($this->keyword,$this->fromuser,$this->touser);
					D('Weixin')->reply_companywx($this->keyword,$this->fromuser,$this->touser);
				}
			}
			//自定义菜单按钮接口
			if($this->type=="event"&&$this->event=="CLICK"){
				if($this->eventkey=="微官网管理"){
					D('Weixin')->diycontrol($this->fromuser,$this->touser);
				}elseif($this->eventkey=="完善简历"){
					D('Weixin')->supplyresume($this->fromuser,$this->touser);
				}else{
					D('Weixin')->reply_companywx($this->eventkey,$this->fromuser,$this->touser);
				}
			}
			//语音接口
			if($this->type == "voice"){
				D('Weixin')->getvoice_companywx(array($this->mediaid,$this->format),$this->fromuser,$this->touser);
			}
			//头像接口
			if($this->type == 'image'){
				D('Weixin')->getface_companywx($this->mediaid,$this->fromuser,$this->touser);
			}
			//地理位置
			if($this->type=='location'){
				//echo D('Weixin')->message->text($this->fromuser,$this->touser,$this->map_x);
				D('Weixin')->location_companywx($this->map_xx,$this->map_yy,$this->fromuser,$this->touser);
			}
			if($this->type=='event'&&$this->event=='LOCATION'){
				//echo D('Weixin')->message->text($this->fromuser,$this->touser,$this->map_x);
				D('Weixin')->location_companywx($this->map_x,$this->map_y,$this->fromuser,$this->touser);
			}
		}
		
		public function wxindex(){
			$cid = $_GET['companyid'];
			$diyinfo = D('Company')->get_company_style($cid);
			$company_hiring = D('Company')->get_company_hiringlist($cid);
			$this->assign('diyinfo',$diyinfo);
			$this->assign('company_hiring',$company_hiring);
			$this->display();
		}
		
		public function read(){
			$article_id = base64_decode($_GET['article_id']);
			$article = explode('-',base64_decode($_GET['article_id']));
			$reply_id = $article[0];
			$article_id = $article[1];
			$reply = D('weixin_reply_image')->where('reply_id = '.$reply_id)->find();
			if($article_id == 0){
				$message = unserialize($reply['content']);
			}else{
				$messageList = unserialize($reply['content_more']);
				$message = $messageList[$article_id];
			}
			$message['updateline'] = $reply['updateline'];
			$this->assign('message',$message);
			$this->display();
		}
		
	
	}
?>
